package com.botu.springcloudsentinelannotion.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author: hanzeyao
 * @Date: 2019/12/7 14:19
 * @Description:
 */
@Slf4j
@Service
public class TestService {

    @SentinelResource(value = "doSomething",blockHandler = "execpHander")
    public String doSomething(String str){
        System.out.println("=====pass=====");
        return str;
    }

    public String execpHander(String str, BlockException ex){
        System.out.println("-----fail-----");
        return "限流了:==="+str+ex.toString();
    }

    @SentinelResource(value = "doSomething1",fallback = "execpHander1")
    public String doSomething1(String str){
        try {
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("=====pass=====");
        return str;
    }

    public String execpHander1(String str, Throwable ex){
        System.out.println("-----fail-----");
        return "降级了:==="+str+ex.toString();
    }

}
